package com.mediacloud.app.newsmodule.fragment.baoliao.adapter

import android.widget.ImageView
import com.bumptech.glide.Glide
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.luck.picture.lib.entity.LocalMedia
import com.mediacloud.app.assembly.util.*
import com.mediacloud.app.newsmodule.R
import com.mediacloud.app.newsmodule.utils.ToastUtil
import java.io.File
import java.text.DecimalFormat
import java.util.*

class BaoLiaoPreImageAdapter :
    BaseQuickAdapter<LocalMedia, BaseViewHolder>(R.layout.dialog_bao_liao_image_pre_item) {

    var totalSize = 0

    override fun convert(helper: BaseViewHolder, media: LocalMedia) {

        val fl = File(media.realPath)
        Glide.with(helper.itemView.context).load(media.compressPath)
            .into(helper.itemView.findViewById<ImageView>(R.id.ivImage))
        helper.setText(R.id.selectNum, "${(helper.position) + 1}/$totalSize")
        helper.setText(R.id.size, "素材大小:" + formetFileSize(fl.length()))
        helper.itemView.findViewById<ImageView>(R.id.delete).setOnClickListener {
            ToastUtil.show(helper.itemView.context, media.fileName)
        }
        helper.addOnClickListener(R.id.delete, R.id.ivImage, R.id.rootView)
    }

    /**
     * 转换文件大小
     * @param fileS
     * @return
     */
    fun formetFileSize(fileS: Long): String? {
        val df = DecimalFormat("#.00")
        var fileSizeString = ""
        val wrongSize = "0B"
        if (fileS == 0L) {
            return wrongSize
        }
        fileSizeString = if (fileS < 1024) {
            df.format(fileS.toDouble()).toString() + "B"
        } else if (fileS < 1048576) {
            df.format(fileS.toDouble() / 1024).toString() + "KB"
        } else if (fileS < 1073741824) {
            df.format(fileS.toDouble() / 1048576).toString() + "MB"
        } else {
            df.format(fileS.toDouble() / 1073741824).toString() + "GB"
        }
        return fileSizeString
    }
}